package com.cavy.platform.client.domain;

import com.cavy.platform.client.BeansProvider;
import com.cavy.platform.client.service.DockerService;
import com.spotify.docker.client.messages.Event;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author 唐旭
 * @Date 2018/7/18
 */
@Data
@Slf4j
public class StageDependency extends ContainerInfo {
    private String linkName;

    /**
     * 创建依赖容器
     * 注册依赖容器停止事件处理器-->停止整个流程,调用移除命令
     * 启动依赖容器
     * 等待依赖容器启动成功
     *
     * @param parameter 运行参数
     * @param index     并行任务的索引
     * @throws ContainerRunningException 容器运行异常
     */
    @Override
    public void run(RunningParameter parameter, int index) throws ContainerRunningException, InterruptedException {
        initContainerTagName(parameter, index);
        //1,初始化 挂载(默认挂载一个工作目录)
        initVolumes(parameter);
        //2,初始化 环境变量(加入job的信息)
        initEnvironments(parameter, index);

        DockerService bean = BeansProvider.getBean(DockerService.class);
        assert bean != null;

        Iterator<ContainerRunCommand> commandList = initContainerRunCommandList(bean, parameter, this);
        while (run && commandList.hasNext()) {
            commandList.next().exec();
        }
    }

    @Override
    protected void initContainerTagName(RunningParameter parameter, int index) {
        super.initContainerTagName(parameter, index);
        containerTagName = containerTagName + ContainerInfo.SPLIT + "StageDependency" + ContainerInfo.SPLIT + index;
    }

    @Override
    public void stopEvent(Event event) {
        //停止主容器
        try {
            threadLocal.get().stop();
        } catch (InterruptedException | ContainerRunningException e) {
            log.error("创建容器发生错误,错误类型:{},错误信息:", e.getClass(), e.getMessage());
        }
        super.stopEvent(event);
    }

    @Override
    public void startEvent(Event event) {
        start = true;
        synchronized (this) {
            this.notifyAll();
        }
    }

    @Override
    public void createEvent(Event event) {
        //空实现,仅为覆盖父类方法
    }

    @Override
    public void destroyEvent(Event event) {
        //空实现,仅为覆盖父类方法
    }

    @Override
    public void killEvent(Event event) {
        //空实现,仅为覆盖父类方法
    }


    @Override
    public void dieEvent(Event event) {
        //空实现,仅为覆盖父类方法
    }

    @Override
    protected Iterator<ContainerRunCommand> initContainerRunCommandList(DockerService bean, RunningParameter parameter, ContainerInfo containerInfo) {
        List<ContainerRunCommand> list = new ArrayList<>(4);
        list.add(new ContainerRunCommand() {
            @Override
            public void exec() throws InterruptedException, ContainerRunningException {
                //创建依赖容器
                bean.createContainer(containerInfo);
            }
        });
        list.add(new ContainerRunCommand() {
            @Override
            public void exec() throws InterruptedException, ContainerRunningException {
                //注册依赖容器停止事件处理器-->停止整个流程,调用移除命令
                bean.registerEventHandler(containerInfo);
            }
        });
        list.add(new ContainerRunCommand() {
            @Override
            public void exec() throws InterruptedException, ContainerRunningException {
                //启动依赖容器
                bean.startContainer(dockerContainerId);
            }
        });
        list.add(new ContainerRunCommand() {
            @Override
            public void exec() throws InterruptedException, ContainerRunningException {
                //等待依赖容器启动成功
                waitStartEvent();
            }
        });
        return list.iterator();
    }
}
